﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BulldogCMS.Core.Storage;
using System.Xml.Linq;
using BulldogCMS.Core.Storage.DataTypes;

namespace BulldogCMS.Core.Tests.Storage
{
    [TestClass]
    public class TreeTest
    {
        [TestMethod]
        public void TreeXMLLoadTest()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Assert.AreEqual(tree.Nodes.Count(), 2, "Count of root nodes is not 2");
        }

        [TestMethod]
        public void GetContentNodeByPath()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node contentNode = tree.GetNode("/Content");
            Assert.IsNotNull(contentNode, "Content node does not exist");
        }

        [TestMethod]
        public void GetDetailsFromContentNode()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node contentNode = tree.GetNode("/Content");

            Assert.AreEqual("/Content", contentNode.Path, "Path is not equal to /Content");
            Assert.AreEqual(contentNode.Name, "Content", "Name is not equal to Content");
            Assert.AreEqual(contentNode.TemplateName, "BulldogCMS.Core.Templates.System.ContentNodeTemplate");
        }

        [TestMethod]
        public void GetPartnersNodeByPath()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node partnersNode = tree.GetNode("/Content/Partners");

            Assert.IsNotNull(partnersNode, "Partners node is null");
        }

        [TestMethod]
        public void GetPartnersNodeByPathProperties()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node partnersNode = tree.GetNode("/Content/Partners");

            Assert.IsNotNull(partnersNode, "Partners node is null");
            Assert.AreEqual<string>("/Content/Partners", partnersNode.Path);
            Assert.AreEqual<string>("Partners", partnersNode.Name);
            Assert.AreEqual<string>("/Content/Partners", partnersNode.CurrentProviderPath);
        }

        [TestMethod]
        public void GetApplePartnerNodeByPathProperties()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node partnerNode = tree.GetNode("/Content/Partners/Apple");

            Assert.IsNotNull(partnerNode, "Partners node is null");
            Assert.AreEqual<string>("/Content/Partners/Apple", partnerNode.Path);
            Assert.AreEqual<string>("Apple", partnerNode.Name);
            Assert.AreEqual<string>("/Content/Partners/Apple", partnerNode.CurrentProviderPath);
        }

        [TestMethod]
        public void ValidateFieldOnNodeTest()
        {
            var tree = BulldogCMS.Core.Context.Application.Tree;
            Node partnerNode = tree.GetNode("/Content/Partners/Apple");
            var templates = Context.Application.Templates;
            //BulldogCMS.Core.Templates.System.ContentNodeTemplate
            var titleTextField = partnerNode.GetValue<TextField>("Title");
            Assert.IsNotNull(titleTextField);
            var nonExistingField = partnerNode.GetValue<TextField>("NonExistingField");
            Assert.IsNull(nonExistingField);
        }


        #region init and cleanup

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            XDocument defaultTreeDoc =              Helper.ReflectionHelper.GetXMLFromAssembly("BulldogCMS.Core.Tests.Resources.Trees.DefaultTestingTree.xml");
            XDocument defaultConfigurationDoc =     Helper.ReflectionHelper.GetXMLFromAssembly("BulldogCMS.Core.Tests.Resources.Configurations.DefaultConfiguration.xml");

            BulldogCMS.Core.Context.InitApplicationContext(defaultConfigurationDoc, defaultTreeDoc);
            BulldogCMS.Core.Context.Application.RequestContextProvider.SetRequestContext(new Runtime.RequestContext() {
                Language = new System.Globalization.CultureInfo("cs-CZ")
            });
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            BulldogCMS.Core.Context.CleanContext();
        }

        #endregion


    }
}
